package com.letu.core.utils;

import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.poi.xwpf.usermodel.Document;
import org.springframework.core.io.ClassPathResource;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassNameFreemarkerUtil
 * @Description TODO
 * @Authoradmin
 * @Date 2022/10/11 16:05
 * @Version 1.0
 **/
public final class FreemarkerUtil {

    public static File replaceTemplateFile(String templateUrl, Map<String,String> replacementMap, String docName) throws Exception {

        String path = "D:\\";
        String templateFileName = docName + System.currentTimeMillis() +  ".docx";
        File templateFile = new File(path + File.separator + templateFileName);
        // 下载网络文件
        int byteread = 0;

        URL url = new URL(templateUrl);

        try {
            URLConnection conn = url.openConnection();
            InputStream inStream = conn.getInputStream();
            FileOutputStream fs = new FileOutputStream(templateFile);

            byte[] buffer = new byte[1204];
            while ((byteread = inStream.read(buffer)) != -1) {
                fs.write(buffer, 0, byteread);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //1.创建配置类
        Configuration configuration=new Configuration(Configuration.getVersion());

        //2.设置模板所在的目录
        configuration.setDirectoryForTemplateLoading(new File(path));
        //3.设置字符集
        configuration.setDefaultEncoding("utf-8");
        //4.加载模板
        Template template = configuration.getTemplate(templateFileName);

        //6.创建Writer对象
        File file = new File(path + File.separator + templateFileName );
        Writer out =new FileWriter(file);
        //7.输出
        template.process(replacementMap, out);
        //8.关闭Writer对象
        out.close();

        return file;

    }

    public static void main(String[] args) throws Exception {
//        //1.创建配置类
//        Configuration configuration=new Configuration(Configuration.getVersion());
////2.设置模板所在的目录
//        configuration.setDirectoryForTemplateLoading(new File("D:\\ftl"));
////3.设置字符集
//        configuration.setDefaultEncoding("utf-8");
////4.加载模板
//        Template template = configuration.getTemplate("test1.docx");
////5.创建数据模型
//        Map map=new HashMap();
//        map.put("name", "张三");
//        map.put("message", "欢迎来使用Freemarker！");
//////6.创建Writer对象
//        Writer out =new FileWriter(new File("D:\\ftl\\hello.docx"));
////7.输出
//        template.process(map, out);
////8.关闭Writer对象
//        out.close();

//        FreemarkerUtil freemarkerUtil = new FreemarkerUtil();
//        freemarkerUtil.replaceTemplateFile("http://10.20.16.78:9090/pvenue/test2.xml", map);

//获取Word模板，模板存放路径在项目的resources目录下
        File file = new File("D:\\TTT.docx");
        InputStream inputStream = new FileInputStream(file);
        //注册xdocreport实例并加载FreeMarker模板引擎
        try {
            IXDocReport report = XDocReportRegistry.getRegistry().loadReport(inputStream, TemplateEngineKind.Freemarker);
            //创建xdocreport上下文对象
            IContext context = report.createContext();
            //创建要替换的文本变量对象
//            TextParamEntity textParamEntity = new TextParamEntity();
//            textParamEntity.setTitle("孙权");
//            textParamEntity.setAuthor("司马光");
            Map<String,String> textParamEntity = new HashMap<String, String>();
            textParamEntity.put("title","lelele");
            textParamEntity.put("author", "liiliii");
            context.put("text", textParamEntity);
            //设置响应内容格式（docx）application/msword（doc）
            OutputStream out = new FileOutputStream(file);
            report.process(context, out);
        } catch (Exception e) {
        }

    }

}
